package org.focusj.scala.colls

object UseStream extends App {
    // way 1 to build a Stream
    //normal way
    val empty = Stream[Int]()
    1 #:: 2 #:: empty foreach print
    //way 2 to build a Stream
    //cons其实不是一个函数，而是一个对象，源码便知
    Stream.cons(1, empty) foreach println

    println("******************************part one**************************************")
    //Stream发力的地方
    //计算斐波那契数列, 这个东西不会让你的cpu飙升到100的
    lazy val fib: Stream[BigInt] = Stream.cons(1, Stream.cons(1, (fib zip fib.tail).map { p => p._1 + p._2 }))
    def i(x: Int, y: Int): Stream[Int] = (x * y) #:: i(x + 1, y * 2)
    println(fib(3))
    i(2, 2).take(3) foreach println
    println("******************************part two**************************************")

    //make an iterator to a Stream
    (1 until 10).iterator.toStream.take(10) foreach println
    //Iteratorable to a Stream
    (1 to 10).toStream.take(3) foreach println

    println("******************************part three**************************************")
    println((7 to 7) take 49 reduceLeft { _ + _ })

    Range(1, 20, 3) foreach println //1
    (1 until 20 by 3).toStream foreach println //2 这两种写法是等效的
}